
// A better interface for the timer stuff

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>


static struct timeval start;
static struct timeval end;
static int started = 0;
static int stopped = 0;
void startTimer() {
  gettimeofday(&start, NULL);
  started = 1;
  stopped = 0;
}

float stopTimer() {
  if (started) {
    gettimeofday(&end, NULL);
    int msecs = (end.tv_sec * 1000000 + end.tv_usec) - 
      (start.tv_sec * 1000000 + start.tv_usec);
    stopped = 1;
    started = 0;
    float secs = msecs / 1000000.0;
    return secs;
  }
  return 0.0;
}

int getElapsedSeconds() {
  if (stopped) {
    return end.tv_sec - start.tv_sec;
  }
  return 0;
}

int getElapsedMicroseconds() {
  if (stopped) {
    return (end.tv_sec * 1000000 + end.tv_usec) - 
      (start.tv_sec * 1000000 + start.tv_usec);
  }

}

